#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5, M=(1<<15)+5, B=1000, C=N/B+5;
int a[N], fa[N];
vector<int> e[N];
int siz[N], sz[N], tag[N], pos[N], tot;
void dfs1(int x, int ff){
	fa[x]=ff;
	siz[x]=1;
	sz[x]=1;
	for(auto y:e[x]){
		if(y==ff) continue;
		dfs1(y, x);
		siz[x]+=siz[y];
		sz[x]+=sz[y];
	}
	if(sz[x]>=B){
		tag[x]=++tot;
		pos[tot]=x;
		sz[x]=0;
	}
}
int ct[C][M], vl[C][N];
void dfs2(int x, int ff, int id){
	vl[id][x]=vl[id][ff]|(1<<a[x]);
	ct[id][vl[id][x]]++;
	for(auto y:e[x]){
		if(y==ff) continue;
		dfs2(y, x, id);
	}
}
void dfs3(int x, int ff, int s, int &res){
	if(!(s&(1<<a[x]))) return;
	res++;
	for(auto y:e[x]){
		if(y==ff) continue;
		dfs3(y, x, s, res);
	}
}
int main(){
	cerr << sizeof(vl)/1024.0/1024 << '\n';
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n, q, c;
	cin >> n >> q >> c;
	for(int i=1; i<=n; ++i){
		cin >> a[i];
	}
	for(int i=2; i<=n; ++i){
		cin >> fa[i];
		e[fa[i]].push_back(i);
		e[i].push_back(fa[i]);
	}
	dfs1(1, 0);
	for(int i=1; i<=tot; ++i){
		dfs2(pos[i], 0, i);
		for(int k=0; k<c; ++k){
			for(int j=0; j<(1<<c); ++j){
				if(!(j&(1<<k))){
					ct[i][j^(1<<k)]+=ct[i][j];
				}
			}
		}
	}
	for(int i=1; i<=q; ++i){
		int s, u;
		cin >> s >> u;
		if(!(s&(1<<a[u]))){
			cout << "1\n";
			continue;
		}
		int ans=0;
		for(int j=1; j<=tot; ++j){
			if((s&vl[j][u])==vl[j][u]){
				ans=max(ans, ct[j][s]);
			}
		}
		if(!ans){
			dfs3(u, 0, s, ans);
		}
		cout << ans << '\n';
	}
	return 0;
}


